//
//  ICMGetCarRepairPartsTableViewCell.m
//  ICrEAM
//
//  Created by FoREVer_SaD on 16/11/1.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMGetCarRepairPartsTableViewCell.h"
#import "PopoverSelectionViewController.h"

@interface ICMGetCarRepairPartsTableViewCell ()<PopoverSelectionViewControllerDelegate,UITextFieldDelegate>

@property (nonatomic, strong) UILabel *operateLabel;

@property (nonatomic, strong) UILabel *orderLabel;

@property (nonatomic, strong) UILabel *partNumberLabel;

@property (nonatomic, strong) UILabel *partNameLabel;

@property (nonatomic, strong) UITextField *quantityTextField;

@property (nonatomic, strong) UILabel *unitPriceLabel;

@property (nonatomic, strong) UILabel *totalPriceLabel;

@property (nonatomic, strong) UILabel *repairTypeLabel;

@property (nonatomic, strong) UILabel *stockPlaceLabel;

@property (nonatomic, strong) UILabel *partPropertyLabel;

@property (nonatomic, strong) UILabel *remainLabel;

@property (nonatomic, strong) UIView *tenthSep;
@property (nonatomic, strong) UIImageView *icon;
@end

@implementation ICMGetCarRepairPartsTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
#pragma mark    初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        UIView *repairPartsCellView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth-ActualWidth(20), ActualHeight(25))];
        repairPartsCellView.backgroundColor = BackgroundColor_Gray;
        [self.contentView addSubview:repairPartsCellView];
        
        UIButton *operateButton = [UIButton buttonWithType:UIButtonTypeCustom];
        operateButton.frame = CGRectMake(0, ActualHeight(5), ActualWidth(50), ActualHeight(15));
        [operateButton addTarget:self action:@selector(clickDeleteButton) forControlEvents:UIControlEventTouchUpInside];
        
        _icon = [[UIImageView alloc] initWithFrame:CGRectMake(ActualWidth(17.5), ActualHeight(5), ActualWidth(15), ActualHeight(15))];
        _icon.image = [UIImage imageNamed:@"btn_tableview_delete"];
        
        [repairPartsCellView addSubview:_icon];
        [repairPartsCellView addSubview:operateButton];
        
        UIView *firstSep = [[UIView alloc] initWithFrame:CGRectMake(operateButton.rightEdge, 0, 1, repairPartsCellView.height)];
        firstSep.backgroundColor = BorderColor_Gray;
        [repairPartsCellView addSubview:firstSep];
        
        UILabel *orderLabel = [self createLabelWithText:[@(self.order) stringValue] frame:CGRectMake(firstSep.rightEdge, operateButton.y, ActualWidth(50), ActualHeight(15)) textAlignment:NSTextAlignmentCenter];
        [repairPartsCellView addSubview:orderLabel];
        self.orderLabel = orderLabel;
        
        UIView *secondSep = [[UIView alloc] initWithFrame:CGRectMake(orderLabel.rightEdge, 0, 1, repairPartsCellView.height)];
        secondSep.backgroundColor = BorderColor_Gray;
        [repairPartsCellView addSubview:secondSep];
        
        UILabel *partNumberLabel = [self createLabelWithText:@"" frame:CGRectMake(secondSep.rightEdge, operateButton.y, ActualWidth(145), ActualHeight(15)) textAlignment:NSTextAlignmentCenter];
        [repairPartsCellView addSubview:partNumberLabel];
        self.partNumberLabel = partNumberLabel;
        
        UIView *thirdSep = [[UIView alloc] initWithFrame:CGRectMake(partNumberLabel.rightEdge, 0, 1, repairPartsCellView.height)];
        thirdSep.backgroundColor = BorderColor_Gray;
        [repairPartsCellView addSubview:thirdSep];
        
        UILabel *partNameLabel = [self createLabelWithText:@"" frame:CGRectMake(thirdSep.rightEdge, operateButton.y, ActualWidth(215), ActualHeight(15)) textAlignment:NSTextAlignmentLeft];
        [repairPartsCellView addSubview:partNameLabel];
        self.partNameLabel = partNameLabel;
        
        UIView *fourthSep = [[UIView alloc] initWithFrame:CGRectMake(partNameLabel.rightEdge, 0, 1, repairPartsCellView.height)];
        fourthSep.backgroundColor = BorderColor_Gray;
        [repairPartsCellView addSubview:fourthSep];
        
        UITextField *quantityTextField = [[UITextField alloc] initWithFrame:CGRectMake(fourthSep.rightEdge, operateButton.y, ActualWidth(75), ActualHeight(15))];
        quantityTextField.keyboardType = UIKeyboardTypeNumberPad;
        quantityTextField.font = ContentFont;
        quantityTextField.textColor = ContentTextColor_Black;
        quantityTextField.textAlignment = NSTextAlignmentRight;
        quantityTextField.delegate = self;
        [repairPartsCellView addSubview:quantityTextField];
        self.quantityTextField = quantityTextField;
        
        UIView *fifthSep = [[UIView alloc] initWithFrame:CGRectMake(quantityTextField.rightEdge, 0, 1, repairPartsCellView.height)];
        fifthSep.backgroundColor = BorderColor_Gray;
        [repairPartsCellView addSubview:fifthSep];
        
        UILabel *unitPriceLabel = [self createLabelWithText:@"" frame:CGRectMake(fifthSep.rightEdge, operateButton.y, ActualWidth(75), ActualHeight(15)) textAlignment:NSTextAlignmentRight];
        [repairPartsCellView addSubview:unitPriceLabel];
        self.unitPriceLabel = unitPriceLabel;
        
        UIView *sixthSep = [[UIView alloc] initWithFrame:CGRectMake(unitPriceLabel.rightEdge, 0, 1, repairPartsCellView.height)];
        sixthSep.backgroundColor = BorderColor_Gray;
        [repairPartsCellView addSubview:sixthSep];
        
        UILabel *totalPriceLabel = [self createLabelWithText:@"" frame:CGRectMake(sixthSep.rightEdge, operateButton.y, ActualWidth(75), ActualHeight(15)) textAlignment:NSTextAlignmentRight];
        [repairPartsCellView addSubview:totalPriceLabel];
        self.totalPriceLabel = totalPriceLabel;
        
        UIView *seventhSep = [[UIView alloc] initWithFrame:CGRectMake(totalPriceLabel.rightEdge, 0, 1, repairPartsCellView.height)];
        seventhSep.backgroundColor = BorderColor_Gray;
        [repairPartsCellView addSubview:seventhSep];
        
        UILabel *repairTypeLabel = [self createLabelWithText:@"" frame:CGRectMake(seventhSep.rightEdge, operateButton.y, ActualWidth(75), ActualHeight(15)) textAlignment:NSTextAlignmentCenter];
        repairTypeLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickRepairType)];
        [repairTypeLabel addGestureRecognizer:tap];
        [repairPartsCellView addSubview:repairTypeLabel];
        self.repairTypeLabel = repairTypeLabel;
        
        UIView *eighthSep = [[UIView alloc] initWithFrame:CGRectMake(repairTypeLabel.rightEdge, 0, 1, repairPartsCellView.height)];
        eighthSep.backgroundColor = BorderColor_Gray;
        [repairPartsCellView addSubview:eighthSep];
        
        UILabel *stockPlaceLabel = [self createLabelWithText:@"" frame:CGRectMake(eighthSep.rightEdge, operateButton.y, ActualWidth(105), ActualHeight(15)) textAlignment:NSTextAlignmentCenter];
        [repairPartsCellView addSubview:stockPlaceLabel];
        self.stockPlaceLabel = stockPlaceLabel;
        
        UIView *ninthSep = [[UIView alloc] initWithFrame:CGRectMake(stockPlaceLabel.rightEdge, 0, 1, repairPartsCellView.height)];
        ninthSep.backgroundColor = BorderColor_Gray;
        [repairPartsCellView addSubview:ninthSep];
        
//        if(self.inType != 0)
//        {
//            UILabel *partPropertyLabel = [self createLabelWithText:@"" frame:CGRectMake(ninthSep.rightEdge, operateButton.y, ScreenWidth-ActualWidth(40)-stockPlaceLabel.rightEdge, ActualHeight(15)) textAlignment:NSTextAlignmentCenter];
//            [repairPartsCellView addSubview:partPropertyLabel];
//            self.partPropertyLabel = partPropertyLabel;
//        }
//        else
//        {
            //预约
            UILabel *partPropertyLabel = [self createLabelWithText:@"" frame:CGRectMake(ninthSep.rightEdge, operateButton.y, ScreenWidth-ActualWidth(40)-ActualWidth(40)-stockPlaceLabel.rightEdge, ActualHeight(15)) textAlignment:NSTextAlignmentCenter];
            [repairPartsCellView addSubview:partPropertyLabel];
            self.partPropertyLabel = partPropertyLabel;
            
            UIView *tenthSep = [[UIView alloc] initWithFrame:CGRectMake(partPropertyLabel.rightEdge, 0, 1, repairPartsCellView.height)];
            tenthSep.backgroundColor = BorderColor_Gray;
            [repairPartsCellView addSubview:tenthSep];
            self.tenthSep = tenthSep;
        
            UILabel *remainLabel = [self createLabelWithText:@"" frame:CGRectMake(tenthSep.rightEdge, operateButton.y, ActualWidth(40), ActualHeight(15)) textAlignment:NSTextAlignmentCenter];
            [repairPartsCellView addSubview:remainLabel];
            self.remainLabel = remainLabel;
//        }
        
    }
    return self;
}

- (void)setInType:(int)inType {
    _inType = inType;
    if (_inType == 0) {
        self.tenthSep.hidden = NO;
        self.remainLabel.hidden = NO;
        self.partPropertyLabel.frame = CGRectMake(self.partPropertyLabel.x, self.partPropertyLabel.y, ScreenWidth-ActualWidth(40)-ActualWidth(40)-self.stockPlaceLabel.rightEdge, self.partPropertyLabel.height);
    } else {
        self.tenthSep.hidden = YES;
        self.remainLabel.hidden = YES;
        self.partPropertyLabel.frame = CGRectMake(self.partPropertyLabel.x, self.partPropertyLabel.y, ScreenWidth-ActualWidth(40)-self.stockPlaceLabel.rightEdge, self.partPropertyLabel.height);
    }
}
#pragma mark    点击维修类型
- (void)clickRepairType{
    [ICrEAMTool showPopoverSelectionViewWithSourceView:self.repairTypeLabel displayWidth:self.repairTypeLabel.width displayHeight:self.height dataArray:self.repairTypes delegate:self showType:PopoverSelectionViewShowTypeTable];
}

- (void)getSelectedTitle:(NSString *)string {
    if(![self.model.UPDATE_STATUS isEqualToString:@"A"])
    {
        self.model.UPDATE_STATUS = @"U";
    }
    self.model.REPAIR_TYPE_NAME = string;
    self.repairTypeLabel.text = string;
}
#pragma mark    点击删除按钮
- (void)clickDeleteButton{
    if ([self.delegate respondsToSelector:@selector(deleteRepairPartsCellAtIndexPath:)]) {
        NSIndexPath *selfIndexPath = [((UITableView *)self.superview.superview) indexPathForCell:self];
        [self.delegate deleteRepairPartsCellAtIndexPath:selfIndexPath];
    }
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    textField.text = [NSString stringWithFormat:@"%.2f", [textField.text floatValue]];
    if(![self.model.UPDATE_STATUS isEqualToString:@"A"])
    {
        self.model.UPDATE_STATUS = @"U";
    }
    if ([textField.text floatValue] <= 0) {
        if ([self.delegate respondsToSelector:@selector(partsQuantitysWillChangeToZero)]) {
            [self.delegate partsQuantitysWillChangeToZero];
        }
        textField.text = self.model.OBLIGATED_QUANTITY;
        return;
    }
    self.model.OBLIGATED_QUANTITY = textField.text;
    if(self.inType == 0){
        if (([self.model.OBLIGATED_QUANTITY floatValue] > [self.model.REAL_QUANTITY floatValue])) {
            if ([self.model.IS_OBLIGATED isEqualToString:ICMEnumIntToString(ICMEnumYESOrNOYES)]) {
                self.model.IS_OBLIGATED = ICMEnumIntToString(ICMEnumYESOrNONO);
                self.remainLabel.text = @"否";
                if ([self.delegate respondsToSelector:@selector(quantityNotEnough:)]) {
                    [self.delegate quantityNotEnough:@"零件数量不足,不能留存!添加的配件数量大于可用库存，将引起负库存。"];
                }
            }else{
                if ([self.delegate respondsToSelector:@selector(quantityNotEnough:)]) {
                    [self.delegate quantityNotEnough:@"添加的配件数量大于可用库存，将引起负库存。"];
                }
            }
            
        }
    }else{
        if (([self.model.OBLIGATED_QUANTITY floatValue] > [self.model.REAL_QUANTITY floatValue])) {
            if ([self.delegate respondsToSelector:@selector(quantityNotEnough:)]) {
                [self.delegate quantityNotEnough:@"添加的配件数量大于可用库存，将引起负库存。"];
            }
        }
    }
    if ([self.delegate respondsToSelector:@selector(didChangePartsQuantitys)]) {
        [self.delegate didChangePartsQuantitys];
    }
}
#pragma mark    设置界面
- (void)setupLabelsWithModel:(ICMRepairPartsModel *)model{
    self.model = model;
    if([model.REPAIR_TYPE_CODE isEqualToString:@"GLGW"]||[model.REPAIR_TYPE_NAME isEqualToString:@"管理工位"]){
        _icon.hidden = YES;
    }else{
        _icon.hidden = NO;
    }
    self.orderLabel.text = [@(self.order+1) stringValue];
    self.partNumberLabel.text = model.PART_NO;
    self.partNameLabel.text = model.PART_NAME;
    self.quantityTextField.text = [NSString stringWithFormat:@"%.2f",[model.OBLIGATED_QUANTITY floatValue]];
    self.unitPriceLabel.text = [NSString stringWithFormat:@"%.2f",[model.SALES_PRICE floatValue]];
    self.totalPriceLabel.text = [NSString stringWithFormat:@"%.2f",[model.PART_SALES_AMOUNT floatValue]];
    self.repairTypeLabel.text = model.REPAIR_TYPE_NAME;
    self.stockPlaceLabel.text = model.STORAGE_POSITION_CODE;
    self.partPropertyLabel.text =[model.OEM_TAG isEqualToString:ICMEnumIntToString(ICMEnumYESOrNOYES)]?@"原装零件":@"非原装零件";
    if(self.inType == 0)
    {
        self.remainLabel.text = [model.IS_OBLIGATED isEqualToString:ICMEnumIntToString(ICMEnumYESOrNOYES)]?@"是":@"否";
    }
    
}

- (UILabel *)createLabelWithText:(NSString *)text frame:(CGRect)frame textAlignment:(NSTextAlignment)textAlignment{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = ContentFont;
    label.textColor = ContentTextColor_Black;
    label.textAlignment = textAlignment;
    return label;
}

@end
